/**
 * 
 */
package eu.news.data.hibernate.pojo;

import java.util.HashSet;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import eu.news.data.pojo.Interface;
import eu.news.data.pojo.News;
import eu.news.data.pojo.Operator;
import eu.news.data.pojo.Sort;
import eu.news.data.pojo.Special;

/**
 * 授权操作者
 * @author WZZ
 *
 */
@Entity
@Table(name="operator")
public class HB_Operator extends Operator implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2382566951180785654L;

	/**
	 * 名称,not null
	 */
	@Id
	String name;
	
	/**
	 * 类型
	 */
	@Column(name="type",columnDefinition="int",nullable=false)
	int type;
	
	/**
	 * 密码，本地用户必须
	 */
	String password;
	
	/**
	 * 新闻
	 */
	@OneToMany(cascade=CascadeType.ALL,mappedBy="operator")
	Collection<HB_Interface> interfaces=new HashSet<HB_Interface>();

	@ManyToMany(
            targetEntity=HB_Sort.class,
            cascade =CascadeType.ALL,
            fetch=FetchType.LAZY
    )
    @JoinTable(
            name="Operator_Sort",
            joinColumns={@JoinColumn(name="operator_id")},
            inverseJoinColumns={@JoinColumn(name="sort_id")}
    )
	Collection<HB_Sort> sorts=new HashSet<HB_Sort>();
	
	@OneToMany(cascade=CascadeType.ALL,mappedBy="operator")
	Collection<HB_News> news=new HashSet<HB_News>();
	
	public HB_Operator(){};
	
	public HB_Operator(String name, int type) {
		super();
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Collection<? extends News> getNews() {
		return news;
	}

	@SuppressWarnings("unchecked")
	public void setNews(Collection<? extends News> news) {
		this.news = (Collection<HB_News>) news;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Collection<? extends Sort> getSorts() {
		return sorts;
	}

	@SuppressWarnings("unchecked")
	public void setSorts(Collection<? extends Sort> sorts) {
		this.sorts = (Collection<HB_Sort>) sorts;
	}
	
	
	public Collection<? extends Interface> getInterfaces() {
		return interfaces;
	}

	@SuppressWarnings("unchecked")
	public void setInterfaces(Collection<? extends Interface> interfaces) {
		this.interfaces = (Collection<HB_Interface>) interfaces;
	}

	/**
	 * Hack 方法，从Sorts中获取Specials
	 * @return
	 */
	public Collection<Special> getSpecials(){
		Collection<Special> specials=new HashSet<Special>();
		for(Sort sort:getSorts()){
			if(!specials.contains(sort.getSpecial())){
				specials.add(sort.getSpecial());
			}
		}
		return specials;
	}
}
